package com.killer_jin.core.common.base;

import com.fasterxml.jackson.annotation.*;
import com.killer_jin.core.common.util.Dates;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import javax.persistence.Transient;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import lombok.Data;

/**
 * BaseDTO 基础DTO,一般用于实体类公共属性
 *
 * @version 1.0
 * @author jiayu 2019-06-19
 */
@Data
public class BasePOJO implements Serializable {
    private static final long serialVersionUID = -4287607489867805101L;

    public static final String FIELD_OPERATE = "operate";
    public static final String FIELD_OBJECT_VERSION_NUMBER = "versionNumber";
    public static final String FIELD_CREATE_BY = "createBy";
    public static final String FIELD_CREATOR = "creator";
    public static final String FIELD_CREATE_DATE = "createDate";
    public static final String FIELD_UPDATE_BY = "updateBy";
    public static final String FIELD_UPDATER = "updater";
    public static final String FIELD_UPDATE_DATE = "updateDate";

    // 操作类型，add/update/delete 参考：{@link Constants.Operation}
    // @Transient是希望该属性不会在数据表中产生字段，但又可以在程序中使用它
    @Transient
    private String _operate;

    // 数据版本号,每发生update则自增,用于实现乐观锁.
    @Transient
    private Long versionNumber;

    //
    // 下面是标准 WHO 字段
    // ----------------------------------------------------------------------------------------------------

    // 创建人用户名
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Long createBy;

    // 创建人名称
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String creator;

    // 创建时间
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonFormat(pattern = Dates.DEFAULT_PATTERN)
    private Date createDate;

    // 更新人用户名
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Long updateBy;

    // 更新人名称
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String updater;

    // 更新时间
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonFormat(pattern = Dates.DEFAULT_PATTERN)
    private Date updateDate;

    // 其它属性
    @JsonIgnore
    @Transient
    protected Map<String, Object> innerMap = new HashMap<>();

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
    }

    public String toJSONString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
    }
}
